package controllers;

import play.Logger;
import play.modules.gae.GAE;
import play.mvc.Before;
import play.mvc.Controller;

public class Login extends Controller{

	@Before
    static void checkConnected() {
		Logger.info("------start user login-----");
		if(GAE.isLoggedIn()){
			Logger.info("request.remoteAddress  >> " + request.remoteAddress);
			boolean flagUserIsAdmin = false;
			if( GAE.getUser().getEmail().equals("hieu.ta@hcp.vn") || GAE.getUser().getEmail().equals("admin@phucuongthinh.com")){
				flagUserIsAdmin = true;
			}
			if(flagUserIsAdmin){
				renderArgs.put("user", GAE.getUser().getEmail());	
			}else{
				renderArgs.put("user", null);
			}
		}
    }
	
	public static void login() {
        GAE.login("Admin.category");
    }
    
    public static void logout() {
        GAE.logout("Application.index");
    }
    
    public static String getUser(){
    	if(renderArgs.get("user") != null && renderArgs.get("user") != ""){
    		return (String)renderArgs.get("user");
    	}
    	return null;
    }

}
